
package fire.eagle.android;

import fire.eagle.android.AndroidApplication;
import fire.eagle.android.Preferences;
import jfireeagle.*;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class BackgroundService extends Service
	implements android.location.LocationListener
{
	private FireEagleClient client;
	private ClientSettings settings;
	private LocationManager locationManager;
	
	static public Intent getIntent(Context ctx)
	{
		Intent i = new Intent();

		ComponentName component = new ComponentName(ctx, BackgroundService.class);

		i.setComponent(component);

		return i;
	}
	
	static public ComponentName start(Context ctx)
	{
		return ctx.startService(getIntent(ctx));
	}
	
	static public boolean stop(Context ctx)
	{
		return ctx.stopService(getIntent(ctx));
	}
	
	static public ComponentName restart(Context ctx)
	{
		stop(ctx);
		return start(ctx);
	}
	
	@Override
	public IBinder onBind(Intent intent)
	{
		return null;
	}
	
	@Override
	public void onCreate()
	{
		
		super.onCreate();
		
		Log.i("Fire Eagle", "background service onCreate called");
		
	}

	
	@Override
	public void onStart(Intent i, int startId)
	{
		super.onStart(i, startId);
		
		Log.i("Fire Eagle", "background service onStart called");
		
		client = AndroidApplication.createFireEagleClient();
		settings = client.getClientSettings();

		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		
		Log.i("Fire Eagle", "background service, transmit frequency = " 
						+ Preferences.getTransmitFrequencyInMilliseconds()
						+ " milliseconds");
		
		if ( (settings.getUserSpecificToken().isValid()) && (Preferences.getTransmitFrequencyInMilliseconds() > 0) )
		{
			
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
								Preferences.getTransmitFrequencyInMilliseconds(), 
								0, 
								this);
		}
		
	}

	@Override
	public void onDestroy()
	{
		Log.i("Fire Eagle", "background service onDestroy called");
		
		super.onDestroy();
		
		shutdown();
	}

	@Override
	public void onLowMemory()
	{
		super.onLowMemory();
		
		shutdown();
	}

	
	public void shutdown()
	{
		shutdownClient();

		if (locationManager != null)
		{
			locationManager.removeUpdates(this);
		}
	
		locationManager = null;
		
		client = null;
		
		settings = null;

	}
	
	
	public void shutdownClient()
	{
		try
		{
			client.shutdown();
		}
		catch (Exception ignored)
		{
			// ignore
		}
	}


	public void onLocationChanged(Location location)
	{
		if (location != null)
		{
			try
			{
				client.updateLocation(location.getLatitude(), location.getLongitude());
			}
			catch (Exception ex)
			{
				Log.w("Fire Eagle background service", "onLocationChanged", ex);
			}
		}
		
	}

	public void onProviderDisabled(String provider)
	{
		// todo : code here?
	}

	public void onProviderEnabled(String provider)
	{
		// todo : code here?
	}

	public void onStatusChanged(String provider, int status, Bundle extras)
	{
		// todo : code here
	}
}
